package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.entity.ShippingAddressEntity;
import com.zw.yzk.kabu.data.network.task.DeleteShippingAddressTask;
import com.zw.yzk.kabu.data.network.task.GetShippingAddressTask;
import com.zw.yzk.kabu.data.network.task.SetDefaultShippingAddressTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.mapper.ShippingAddressMapper;
import com.zw.yzk.kabu.view.view.ShippingAddressView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class ShippingAddressPresenter implements Presenter {

    private UseCase getAddressList;
    private UseCase setDefaultAddress;
    private UseCase deleteAddress;
    private AppCompatActivity activity;
    private ShippingAddressView addressView;
    private ShippingAddressMapper mapper;

    @Inject
    public ShippingAddressPresenter(@Named("getShippingAddressUseCase") UseCase getAddressList,
                                    @Named("setDefaultShippingAddressUseCase") UseCase setDefaultAddress,
                                    @Named("deleteShippingAddressUseCase") UseCase deleteAddress,
                                    AppCompatActivity activity, ShippingAddressMapper mapper) {
        this.getAddressList = getAddressList;
        this.setDefaultAddress = setDefaultAddress;
        this.deleteAddress = deleteAddress;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(ShippingAddressView view) {
        this.addressView = view;
    }

    public void getShippingAddress() {
        getAddressList.execute(new ShippingAddressSubscriber(activity), new GetShippingAddressTask(null));
    }

    public void setDefaultAddress(int id) {
        setDefaultAddress.execute(new SetDefaultAddressSubscriber(activity), new SetDefaultShippingAddressTask(id));
    }

    public void deleteAddress(int id) {
        deleteAddress.execute(new DeleteShippingAddressSubscriber(activity), new DeleteShippingAddressTask(id));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getAddressList.unSubscribe();
        setDefaultAddress.unSubscribe();
        deleteAddress.unSubscribe();
    }

    private class DeleteShippingAddressSubscriber extends BaseSubscriber<ShippingAddressEntity> {

        public DeleteShippingAddressSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(ShippingAddressEntity shippingAddressEntity) {
            super.onNext(shippingAddressEntity);
            addressView.getAddressSuccess(mapper.transfer(shippingAddressEntity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.delete_shipping_address_failed));
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.delete_shipping_address_failed);
        }
    }

    private class SetDefaultAddressSubscriber extends BaseSubscriber<Entity> {

        public SetDefaultAddressSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            getShippingAddress();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.set_default_shipping_address_failed);
        }
    }

    private class ShippingAddressSubscriber extends BaseSubscriber<ShippingAddressEntity> {

        public ShippingAddressSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onNext(ShippingAddressEntity shippingAddressEntity) {
            super.onNext(shippingAddressEntity);
            addressView.getAddressSuccess(mapper.transfer(shippingAddressEntity));
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_shipping_address_failed);
        }
    }
}
